/*
 * @filename CustomLoginSuccessHandler.java
 * 
 * @author yinqi
 * 
 * @version 0.0.1
 * 
 * @date 2017年7月31日
 */
package com.bnzj.core.web.security;

import com.bnzj.core.util.DateUtils;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * 登录成功后的处理
 * 
 * @author yinqi
 */
//@Component
public class CustomLoginSuccessHandler extends SavedRequestAwareAuthenticationSuccessHandler {

    @Override
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,
        Authentication authentication) throws IOException, ServletException {
        // 获得授权后可得到用户信息
        UserDetails userDetails = (UserDetails)authentication.getPrincipal();
        // 该方法会去找登录前的请求页面，如果存在，登录成功后会跳转到之前的页面
        HttpSession session = request.getSession();
        session.setAttribute("loginName", userDetails.getUsername());
        session.setAttribute("loginTime", DateUtils.getNowTimeStr());
        // 该方法会去找登录前的请求页面，如果存在，登录成功后会跳转到之前的页面
        super.onAuthenticationSuccess(request, response, authentication);
    }
}
